package com.teamTPZoo.Context;

import com.teamTPZoo.Constants.ConstantsIF;
import com.teamTPZoo.Util.Cookies;
import com.teamTPZoo.Util.Strings;

import org.apache.log4j.Logger;

import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; 

public class TPZooContext extends HttpServlet
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Logger             logger        = Logger.getLogger( getClass() );
	
    private HttpServletRequest req           = null;
    private Cookies            cookies       = null;
    
	private String             LocalIP       = "";   
	private String             LocalName     = "";   
	private int                LocalPort     = 0;    
	private String             ServerName    = "";   
	private int                ServerPort    = 0;    
	private String             Method        = "";
	private String             Protocol      = "";      
	private String             Scheme        = "";      
	
	private String             QueryString   = "";      
	private String             RequestURI    = "";      
	private StringBuffer       RequestURL    = null;
	
	private String             ContextPath   = "";      
	private String             ServletPath   = "";      
	private Locale             Locale        = null;    

	private String             UserIP        = "";      
	private String             UserHost      = "";      
	private int                UserPort      = 0;       
	
	private boolean            isSecure      = false;    // request.isSecure()    * https와 같은 보안 채널의 사용 여부(true/false 값으로 되어 있음)
	
	private boolean            useFilter     = true;
	
	public  TPZooContext( HttpServletRequest request, boolean setUseFilter ) throws ServletException, IOException
	{
		try {
			this.req     = request;
			this.cookies = new Cookies(request);
			useFilter    = setUseFilter;
			
			
			this.setLocalIP(request.getLocalAddr());
			this.setLocalName(request.getLocalName());
			this.setLocalPort(request.getLocalPort());
			this.setServerName(request.getServerName());
			this.setServerPort(request.getServerPort());
			this.setMethod(request.getMethod());
			this.setProtocol(request.getProtocol());
			this.setScheme(request.getScheme());
			this.setQueryString(request.getQueryString());
			this.setRequestURI(request.getRequestURI());
			this.setRequestURL(request.getRequestURL());
			this.setContextPath(request.getContextPath());
			this.setServletPath(request.getServletPath());
			this.setLocale(request.getLocale());
			this.setUserIP(request.getRemoteAddr());
			this.setUserHost(request.getRemoteHost());
			this.setUserPort(request.getRemotePort());
		}
		catch ( Exception e ) {
			logger.debug( e.toString() );
		}
		finally {
		
		}
	}
	
	public String getParameter( String parameterName )
	{
    	String returnValue = "";
    	
		try {
			returnValue = req.getParameter(parameterName);
		}
		catch ( Exception e ) {
			logger.debug( e.toString() );
		}
		finally {
		
		}
    	
    	return returnValue;    	
	}
	
	public String getParameter( String parameterName, String defaultValue )
	{
    	String returnValue = "";
    	
		try {
			returnValue = Strings.nvl( req.getParameter(parameterName), defaultValue );
		}
		catch ( Exception e ) {
			logger.debug( e.toString() );
		}
		finally {
		
		}
    	
    	return returnValue;    	
	}
	
    public String get( String parameterName ) throws IOException 
    {
    	String returnValue = "";
    	
		try {
	    	if ( parameterName.substring(0, 2).equals("S_" ) ) {
	    		String  tmp = req.getSession().getAttribute(parameterName).toString();
	    		returnValue = (tmp!=null)?tmp:null;
	    	}
	    	else if ( parameterName.substring(0, 2).equals("C_" ) ) { 
	    		returnValue = cookies.getValue(parameterName);
	    	}
	    	else {
	    		returnValue = req.getAttribute(parameterName).toString();
	    	}
		}
		catch ( Exception e ) {
			logger.debug( e.toString() );
		}
		finally {
		
		}
    	
    	return returnValue;    	
    }
    
    public void put( String parameterName, String parameterValue )
    {
		try {
	    	if ( parameterName.substring(0, 2).equals("S_" ) ) {
	    		req.getSession().setAttribute(parameterName, parameterValue);
	    	}
	    	else if ( parameterName.substring(0, 2).equals("C_" ) ) {
	    		Cookies.createCookie(parameterName, parameterValue, ConstantsIF.DOMAIN, ConstantsIF.COOKIE_PATH, ConstantsIF.COOKIE_EXPIRED_TIME);
	    	}
	    	else {
	    		req.setAttribute(parameterName, parameterValue);
	    	}
		}
		catch ( Exception e ) {
			logger.debug( e.toString() );
		}
		finally {
		
		}
    }

    public String getProperties( String propertyName )    
    {    
    	String returnValue = "";       
    	
    	Properties properties = new Properties();	    
    	try {	        
    		properties.load(new FileInputStream("teamTPZoo.properties"));
    		returnValue = properties.getProperty(propertyName, "").toString();
    	} 
    	catch ( FileNotFoundException e ) {
    		e.printStackTrace();	    
    	} catch (IOException e) {
    		e.printStackTrace();
    	}

    	return returnValue;
    }

    
	public String getContextPath() {
		return ContextPath;
	}

	public void setContextPath(String contextPath) {
		ContextPath = contextPath;
	}

	public boolean isSecure() {
		return isSecure;
	}

	public void setSecure(boolean isSecure) {
		this.isSecure = isSecure;
	}

	public Locale getLocale() {
		return Locale;
	}

	public void setLocale(Locale locale) {
		Locale = locale;
	}

	public String getLocalIP() {
		return LocalIP;
	}

	public void setLocalIP(String localIP) {
		LocalIP = localIP;
	}

	public String getLocalName() {
		return LocalName;
	}

	public void setLocalName(String localName) {
		LocalName = localName;
	}

	public int getLocalPort() {
		return LocalPort;
	}

	public void setLocalPort(int localPort) {
		LocalPort = localPort;
	}

	public Logger getLogger() {
		return logger;
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	public String getMethod() {
		return Method;
	}

	public void setMethod(String method) {
		Method = method;
	}

	public String getProtocol() {
		return Protocol;
	}

	public void setProtocol(String protocol) {
		Protocol = protocol;
	}

	public String getQueryString() {
		return QueryString;
	}

	public void setQueryString(String queryString) {
		QueryString = queryString;
	}

	public String getRequestURI() {
		return RequestURI;
	}

	public void setRequestURI(String requestURI) {
		RequestURI = requestURI;
	}

	public StringBuffer getRequestURL() {
		return RequestURL;
	}

	public void setRequestURL(StringBuffer requestURL) {
		RequestURL = requestURL;
	}

	public String getScheme() {
		return Scheme;
	}

	public void setScheme(String scheme) {
		Scheme = scheme;
	}

	public String getServerName() {
		return ServerName;
	}

	public void setServerName(String serverName) {
		ServerName = serverName;
	}

	public int getServerPort() {
		return ServerPort;
	}

	public void setServerPort(int serverPort) {
		ServerPort = serverPort;
	}

	public String getServletPath() {
		return ServletPath;
	}

	public void setServletPath(String servletPath) {
		ServletPath = servletPath;
	}

	public boolean isUseFilter() {
		return useFilter;
	}

	public void setUseFilter(boolean useFilter) {
		this.useFilter = useFilter;
	}

	public String getUserHost() {
		return UserHost;
	}

	public void setUserHost(String userHost) {
		UserHost = userHost;
	}

	public String getUserIP() {
		return UserIP;
	}

	public void setUserIP(String userIP) {
		UserIP = userIP;
	}

	public int getUserPort() {
		return UserPort;
	}

	public void setUserPort(int userPort) {
		UserPort = userPort;
	}
}
